package com.search;


import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.model.TaskInfo;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class TestMeiliIIndex {
    public static void main(String[] args) throws Exception {

        JSONArray array = new JSONArray();
        ArrayList items = new ArrayList() {{
            add(new JSONObject().put("id", "1").put("title", "Carol").put("genres", new JSONArray("[\"Romance\",\"Drama\"]")));
            add(new JSONObject().put("id", "2").put("title", "Wonder Woman").put("genres", new JSONArray("[\"Action\",\"Adventure\"]")));
            add(new JSONObject().put("id", "3").put("title", "Life of Pi").put("genres", new JSONArray("[\"Adventure\",\"Drama\"]")));
            add(new JSONObject().put("id", "4").put("title", "Mad Max: Fury Road").put("genres", new JSONArray("[\"Adventure\",\"Science Fiction\"]")));
            add(new JSONObject().put("id", "5").put("title", "Moana").put("genres", new JSONArray("[\"Fantasy\",\"Action\"]")));
            add(new JSONObject().put("id", "6").put("title", "Philadelphia").put("genres", new JSONArray("[\"Drama\"]")));
        }};

        array.put(items);
        String documents = array.getJSONArray(0).toString();
        Client client = new Client(new Config("http://localhost:7700", "sWzBHtP_HmcsufeksUfIZvRxuskQgxZ58KGK9CB5_1Q"));

        // An index is where the documents are stored.
        TaskInfo taskInfo = client.createIndex("movies","id");

        Index index = client.getIndex("movies");
        // If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.
        index.addDocuments(documents); // => { "taskUid": 0 }
    }
}